import { Component, OnInit } from '@angular/core';
import {News} from '../../../model/News';
import {NewsService} from '../../../services/news.service';
import {Router} from '@angular/router';
import {ToastService} from '../../../services/toast.service';
import {ConfigService} from '../../../services/config.service';

@Component({
  selector: 'app-newslist',
  templateUrl: './newslist.component.html',
  styleUrls: ['./newslist.component.css']
})
export class NewslistComponent implements OnInit {
  nl:Array<News>;
  page:number;
  pagesize:number;
  total:number;
  constructor(private newsservice:NewsService
  ,private router:Router
  ,private toastservice:ToastService
    ,public config:ConfigService

  ) {
    this.nl=new Array();
    this.page=0;
    this.pagesize=20;
    this.total=0;
    this.loadNews(this.page);
  }

  ngOnInit(): void {
  }

  loadNews(page:number){
    this.newsservice.getNewsListByPage(page,this.pagesize)
      .then((data:any)=>{
        if(data.msg='ok') {
          this.nl = new Array();
          this.nl = data.obj.content;
          this.page = data.obj.pageable.pageNumber + 1;
          this.total = data.obj.totalElements;
        }
      })
  }

onPageChange(e){
    console.dir(e);
    let p:number=e-1;

    this.loadNews(p);
}

add(){
    this.router.navigate(['/admin/newsedit',{"optype":'add'}]);
}

delete(id:string){
    this.newsservice.deleteNews(id)
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.loadNews(this.page);
        }
        else {
          //提示错误信息
          this.toastservice.showError('删除信息异常，请刷新后重试');
        }

      })
}

update(id:string){
  this.router.navigate(['/admin/newsedit',{"optype":'update',"id":id}]);
}


}
